import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, ModalService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { GlobalService } from '../../../../cores/services';
import { AccountService } from '../../services/account.service';
import { Pay } from './../../interfaces/inventory.interfaces';
import { skipWhile, switchMap } from 'rxjs/operators';
import { ModalpayComponent } from './modalpay.component';


@Component({
    selector: 'app-pay',
    templateUrl: './pay.component.html',
    styleUrls: ['./pay.component.scss']
})
export class PayComponent implements OnInit {

    theads = ['#', '供应商名称', '期初日期', '应付单据数', '应付总金额 ', '操作'];

    theadtwo = ['#', '单据编号', '原单金额', '原单编号', '原单日期 ', '原单签收人', '原单附件', '操作'];
    search = new SearchParams({ start: '', end: '' });

    list = new Array<Pay>();

    loading = false;
    pay: Pay = {};
    pagination = new Pagination();

    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        public global: GlobalService,
        private confirm: ConfirmService,
        private accountService: AccountService,
        private toast: ToastService,
        private modal: ModalService,
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/admin/platform'))
            .subscribe(() => this.loadDatas());
        this.loadDatas();
    }

    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }

    loadDatas() {
        this.loading = true;
        this.accountService.paymentlist(this.pagination, this.search).subscribe({
            next: res => {
                this.pagination.total = res.datas.total;
                this.list = res.datas.rows;
            },
            complete: () => this.loading = false
        });
    }
    showEditModal() {
        const modal = this.modal.create(ModalpayComponent, { center: true });
        // modal.instance.voucher = voucher;
        modal.open().subscribe(() => {
            this.toast.success('保存成功', '成功保存活动');
            this.loadDatas();
        });
    }
    confirmDelete(id: number) {
        this.confirm.danger('删除确认', `您确认删除应付数据'吗？`)
            .pipe(switchMap<void, ApiData>(() => this.accountService.delelpayment(id)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(this.pay), 1);
                this.toast.success('删除成功', `成功删除应付数据`);
                this.loadDatas();
            });
    }
}
